
$(document).ready(function() {
    BuidingTable();
});

function ClearExcute()
{
    $('#name').val("");
    BuidingTable();
}
function BuidingTable(){
    var ReaderRequestUrl='/ScoreSource/table'
    if ($('#name').val() != null && $('#name').val() != null) {
        ReaderRequestUrl += "?key=";
        ReaderRequestUrl += $('#name').val();
    }
    $('#dataTables').DataTable({
        dom:"tlp",
        destroy:true,
        ajax: {
            url: ReaderRequestUrl,
            dataSrc: ''
        },
        columns: [
            { "data": "_id" },
            { "data": "score_source_code"},
            { "data": "score_source_name"},
            { "data": "createday"},
            { defaultContent:''}
        ],
        columnDefs: [{
                targets: -1,
                align: 'center',
                render: function(data, type, row){
                    return ""
                        +"<a href='javascript:OpenUpdate(" + row._id+");' title='修改'>"
                        +"<i class='glyphicon glyphicon-pencil' style='margin-right: 20px'></i>"
                        +"</a>"
                        +"<a href='javascript:DeleteRow(" +row._id+");' title='删除'>"
                        +"<i class='glyphicon glyphicon-trash text-danger'></i>"
                        +"</a>";
                }
            }],
        oLanguage: {
            sLengthMenu: "每页显示 _MENU_ 条记录",
            sZeroRecords: "抱歉， 数据为空",
            sInfoEmpty: "没有数据",
            sInfoFiltered: "(从 _MAX_ 条数据中检索)",
            oPaginate: {
                sFirst: "首页",
                sPrevious: "前一页",
                sNext: "后一页",
                sLast: "尾页"
            },
        }
    });
}
function openModalAdd(){
    $('#m_modal_add').find("input").each(
        function()
        {
            $(this).val('');
        }
    );
    $('#m_modal_Add').modal('show');
}

function CommitSave() {
    if (!InputCheck()) {
        return;
    }
    var CommitData = {};
    $('#m_modal_add').find("input").each(
        function () {
            CommitData[$(this).attr('id')] = $(this).val();
        }
    );
    $.ajax({
        cache: true,
        type: "POST",
        url: "ScoreSource/Add",
        data: CommitData,
        error: function (request) {
            alert("添加书籍失败...");
        },
        success: function (result) {
            var Curedata = $.extend(true, [], result);
            if (Curedata.state == "0") {
                $('#m_modal_add').modal('hide');
                location.reload();
                $('#m_modal_add').find("input").each(
                    function () {
                        $(this).val("");
                    }
                );
            } else {
                alert(Curedata.msg);
            }
        }
    });
}

function OpenUpdate(_id){
    var ReaderUrl = "ScoreSource/ReaderOne?_id=" + _id;
    $.ajax(
        {
            type : "POST",
            contentType : "application/json",
            url : ReaderUrl,
            dataType : 'json',
            success : function(result)
            {
                var Curedata = $.extend(true, [], result);
                if (Curedata.state == "0")
                {
                    $('#m_modal_edit').find("input").each(function()
                    {
                        $('#m_modal_edit').find("#" + $(this).attr('id')).val(Curedata.data[0][$(this).attr('id')]);
                    });
                    $('#m_modal_edit').modal('show');
                }
                else
                {
                    alert(Curedata.msg);
                }
            }
        });
}

/************************************************************************
 * 提交修改
 ************************************************************************/
function CommitEdit(){
    if(!InputCheckEdit()){
        return;
    }
    var CommitData = {};
    $('#m_modal_edit').find("input").each(
        function()
        {
            CommitData[$(this).attr('id')]=$(this).val();
        }
    );
    $.ajax({
        cache: true,
        type: "POST",
        url:"ScoreSource/CommitEdit",
        data:CommitData,
        error: function(request)
        {
            alert("修改信用分来源失败...");
        },
        success: function(result)
        {
            var Curedata = $.extend(true, [],result);
            if(Curedata.state == "0")
            {
                $('#m_modal_edit').modal('hide');
                alert("修改成功")
                BuidingTable();
                $('#m_modal_edit').find("input").each(
                    function()
                    {
                        $(this).val("");
                    }
                );
            }
            else
            {
                alert(Curedata.msg);
            }
        }
    });
}
/************************************************************************
 * 提交删除
 ************************************************************************/
function DeleteRow(delid)
{
    if(confirm("您确定要删除书籍吗？"))
    {
        var CommitUrl = "ScoreSource/Remove?_id=" + delid;
        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: CommitUrl,
            dataType: 'json',
            success: function(result)
            {
                var Curedata = $.extend(true, [],result);
                if(Curedata.state == "0")
                {
                    alert("删除成功")
                    BuidingTable();
                }
                else
                {
                    alert(Curedata.msg);
                }
            }
        });
    }
}
/************************************************************************
 * 输入检查--添加的页面
 ************************************************************************/
function InputCheck(){
    var CheckString = "";
    $('#m_modal_add').find("input").each(
        function(){
            if($(this).attr('validation') && ($(this).val()==null || $(this).val()=='' || $(this).val()=='undefined')){
                CheckString += $(this).attr('placeholder');
                CheckString += "\r\n";
            }
        }
    );
    //********************************************************************************************************************
    if(CheckString)
    {
        alert(CheckString);
        return false;
    }
    else{
        return true;
    }
}
/************************************************************************
 * 输入检查--修改的页面
 ************************************************************************/
function InputCheckEdit(){
    var CheckString = "";
    $('#m_modal_edit').find("input").each(
        function(){
            if($(this).attr('validation') && ($(this).val()==null || $(this).val()=='' || $(this).val()=='undefined')){
                CheckString += $(this).attr('placeholder');
                CheckString += "\r\n";
            }
        }
    );
    //********************************************************************************************************************
    if(CheckString){
        alert(CheckString);
        return false;
    }
    else{
        return true;
    }
}